package com.iflytek.iot.okhttp.component;


import com.alibaba.fastjson.JSON;
import com.iflytek.iot.okhttp.config.AbsRestTemplateConfig;
import com.iflytek.iot.okhttp.template.CustomerRestTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.converter.StringHttpMessageConverter;

import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;
import java.util.function.Consumer;

/**
 * 针对restTemplate封装的http客服端
 *
 * @author yu
 */
@Configuration
@ConditionalOnMissingBean(CustomerRestTemplate.class)
public class RestTemplateClient extends AbsRestTemplateConfig {

    private static final Logger LOGGER = LoggerFactory.getLogger(RestTemplateClient.class);

    @Resource
    private CustomerRestTemplate customerRestTemplate;

    /**
     * get带请求头的请求
     *
     * @param url                 请求地址
     * @param httpHeadersConsumer 请求头
     * @param responseType        响应类型
     * @param uriVariables        连接参数
     * @return return T
     */
    public <T> T getForObject(String url, Consumer<HttpHeaders> httpHeadersConsumer, Class<T> responseType,
                              Object... uriVariables) {
        customerRestTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
        return customerRestTemplate.getForObject(url, httpHeadersConsumer, responseType, uriVariables);
    }

    /**
     * get不带请求头
     *
     * @param url          请求地址
     * @param responseType 响应类型
     * @param uriVariables url附带惨呼
     * @return return T
     */
    public <T> T getForObject(String url, Class<T> responseType, Object... uriVariables) {
        long startTime = System.currentTimeMillis();
        customerRestTemplate.printCurlLog(url, HttpMethod.GET, null, null, uriVariables);
        customerRestTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
        T result = customerRestTemplate.getForObject(url, responseType, uriVariables);
        if (LOGGER.isDebugEnabled()) {
            long endTime = System.currentTimeMillis();
            LOGGER.debug("The  {}  costTime:{} ms , response is: {}", url, (endTime - startTime), JSON.toJSONString(result));
        }
        return result;
    }


    /**
     * post请求
     *
     * @param url          请求url
     * @param request      请求体
     * @param responseType 返回类型
     * @param uriVariables url参数
     * @return return T
     */
    public <T> T postForObject(String url, Object request, Class<T> responseType, Object... uriVariables) {
        long startTime = System.currentTimeMillis();
        customerRestTemplate.printCurlLog(url, HttpMethod.POST, null, request, uriVariables);
        customerRestTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
        T result = customerRestTemplate.postForObject(url, request, responseType, uriVariables);
        if (LOGGER.isDebugEnabled()) {
            long endTime = System.currentTimeMillis();
            LOGGER.debug("The  {}  costTime:{} ms , response is: {}", url, (endTime - startTime), JSON.toJSONString(result));
        }
        return result;
    }

    /**
     * post请求
     *
     * @param url                 请求url
     * @param httpHeadersConsumer 请求头
     * @param request             请求体
     * @param responseType        返回类型
     * @param uriVariables        url参数
     * @return return T
     */
    public <T> T postForObject(String url, Consumer<HttpHeaders> httpHeadersConsumer, Object request, Class<T> responseType,
                               Object... uriVariables) {
        customerRestTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
        return customerRestTemplate.postForObject(url, httpHeadersConsumer, request, responseType, uriVariables);
    }

}
